<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <head>
	<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2007. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
    <link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <title>
      Using code templates
    </title>
  </head>
  <body>
    <h1>
      Using code templates
    </h1>
    <p>
      In this section you will use content assist to fill in a template for a common loop structure. 
      Open <em>junit.samples/VectorTest.java</em> file in the Java editor if you do not already 
      have it open.
    </p>
    <ol>
      <li>
        Start adding a new method by typing the following:<br>
        <p><code>
        public void testValues() {<br>
        &nbsp;&nbsp;&nbsp; Integer[] expected= new Integer[3];<br>
        &nbsp;&nbsp;&nbsp; for<br>
        </code></p>
      </li>
      <li>
        With the cursor at the end of <code>for</code>, press <kbd>Ctrl+Space</kbd> to enable content assist.&nbsp;
        You will see a list of common templates for &quot;for&quot; loops.&nbsp;
        When you single-click a template, or select it with the <kbd>Up</kbd> or <kbd>Down</kbd>
        arrow keys, you'll see the code for the template in
        its help message.&nbsp; Note that the local array name is guessed automatically.
		<p>
		<img src="images/editor_vectortest_for_template.png"  alt="Content assist for for">
		</p>
      </li>
      <li>
        Choose the <code>for - iterate over array</code>  entry and press <kbd>Enter</kbd> to confirm the template.&nbsp;
        The template will be inserted in your source code.
		<p>
		<img src="images/editor_vectortest_for_template_exp.png"  alt="Inserted for template">
		</p>
      </li>
      <li>
        Next we change the name of the index variable from <em>i</em> to <em>e</em>. To do so simply press <kbd>e</kbd>, as
        the index variable is automatically selected. 
        Observe that the name of the index variable changes at all places. When inserting a template all references to the same variable
        are connected to each other. So changing one changes all the other values 
        as well.
		<p>
		<img src="images/editor_vectortest_for_template_exp_e.png"  alt="Altered for template">
		</p>
      </li>
      <li>
        Pressing the <kbd>tab</kbd> key moves the cursor to the next variable of the code template. This is the array
        <em>expected</em>.
		<p>
		<img src="images/editor_vectortest_for_template_exp_expected.png"  alt="Altered for template">
		</p>
        Since we don't want to change the name (it was guessed right by the template) we press
        tab again, which leaves the template since there aren't any variables left to edit.
      </li>
      <li>
        Complete the <code>for</code> loop as follows:<br>
        <p><code>
        for (int e= 0; e &lt; expected.length; e++) {<br>
        &nbsp;&nbsp;&nbsp; expected[e]= new Integer(e + 1);<br>
        }<br>
        Integer[] actual= to<br>
        </code></p>
      </li>
      <li>
        With the cursor at the end of <code>to</code>, press <kbd>Ctrl+Space</kbd> to enable content assist. Pick <code>toarray - convert collection to array</code>
        and press <kbd>Enter</kbd> to confirm the selection (or double-click the
        selection).
		<p>
        <img src="images/editor_vectortest_toarray_template.png"  alt="toarray template">
		</p>
		The template is inserted in the editor and <code>type</code> is highlighted and selected.
		<p>
		<img src="images/editor_vectortest_toarray_template_exp.png"  alt="Inserted toarray template">
		</p>
      </li>
      <li>
        Overwrite the selection by typing <code>Integer</code>. The type of array constructor changes
        when you change the selection.
      </li>
      <li>
        Press <kbd>Tab</kbd> to move the selection to <code>collection</code> and
        overwrite it by typing <code>fFull</code>.
		<p>
		<img src="images/editor_vectortest_toarray_template_integer.png"  alt="Inserted toarray template">
		</p>
      </li>
      <li>
        Add a semicolon and the following lines of code to complete the method:<br>
        <p><code>
        assertEquals(expected.length, actual.length);<br>
        for (int i= 0; i &lt; actual.length; i++)<br>
        &nbsp;&nbsp;&nbsp; assertEquals(expected[i], actual[i]);<br>
        </code></p>
      </li>
      <li>
        Save the file.
      </li>
    </ol>
    <P class="nav_footer" id="nav_footer">Next Section: <A href="qs-OrganizeImports.htm">Organizing import statements</A>
</P>
    <h3 class="related">Related concepts</h3>
       <a href="../concepts/concept-java-editor.htm">Java editor</a><br>
       <a href="../concepts/concept-editor-templates.htm">Templates</a>

    <h3 class="related">Related references</h3>
       <a href="../reference/preferences/java/editor/ref-preferences-templates.htm">Templates Preferences</a><br>
       <a href="../reference/preferences/java/ref-preferences-editor.htm">Java Editor Preferences</a>

  </body>
</html>

